package com.buka.service.impl;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buka.APi.PayApi;
import com.buka.dao.OrderDao;
import com.buka.entity.Order;
import com.buka.entity.Pay;
import com.buka.service.OrderService;
import com.buka.service.OrderTccAction;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * (Order)表服务实现类
 *
 * @author makejava
 * @since 2025-07-14 21:08:24
 */
@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, Order> implements OrderService {

    @Resource
    PayApi payApi;

    @Resource
    OrderTccAction orderTccAction;

    @Override
//    @GlobalTransactional
    public boolean save(Order entity) {
        Pay pay = new Pay();
        pay.setManey(new BigDecimal(100));
        pay.setCreateat(new Date());
        payApi.insert(pay);
        return super.save(entity);
    }

    @Override
    @GlobalTransactional
    public boolean saveByTCC(){
        // 1. 调用 Order TCC 接口（创建订单-预留状态）
        // 2. 设置订单信息
        Order order = new Order();
        order.setCreateat(new Date());
        long l = System.currentTimeMillis();
        order.setId(l);
        boolean b = orderTccAction.prepareCreateOrder(null, order);

        // 3. 调用 Pay TCC 接口（创建支付信息-预留状态）
        Pay pay = new Pay();
        pay.setManey(new BigDecimal(100));
        pay.setCreateat(new Date());
        R r = payApi.savePayByTCC(pay);
        Boolean data = (Boolean) r.getData();
        return data && b;
    }

}

